home *** CD-ROM | disk | FTP | other *** search
- /*
- * (c) Copyright 1993-94, Silicon Graphics, Inc.
- * ALL RIGHTS RESERVED
- *
- * Permission to use, copy, modify, and distribute this software for
- * any purpose and without fee is hereby granted, provided that the above
- * copyright notice appear in all copies and that both the copyright notice
- * and this permission notice appear in supporting documentation, and that
- * the name of Silicon Graphics, Inc. not be used in advertising
- * or publicity pertaining to distribution of the software without specific,
- * written prior permission.
- *
- * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
- * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
- * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
- * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
- * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
- * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
- * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
- * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
- * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
- * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
- * Use, duplication, or disclosure by the Government is subject to
- * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
- * (c)(1)(ii) of the Rights in Technical Data and Computer Software
- * clause at DFARS 252.227-7013 and/or in similar or successor
- * clauses in the FAR or the DOD or NASA FAR Supplement.
- * Unpublished-- rights reserved under the copyright laws of the
- * United States. Contractor/manufacturer is Silicon Graphics,
- * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
- *
- * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
- */
- #include <GL/glx.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <X11/keysym.h>
-
- static int attributes[] = {
- GLX_RGBA,
- GLX_RED_SIZE, 1,
- GLX_GREEN_SIZE, 1,
- GLX_BLUE_SIZE, 1,
- None,
- };
-
- int width = 350, height = 450;
-
- static GLfloat cwa[2] = { 0, -1 };
- static GLfloat cwb[2] = { -1, .5 };
- static GLfloat cwc[2] = { 1, 1 };
- static GLfloat ccwa[2] = { 0, -1 };
- static GLfloat ccwb[2] = { 1, .5 };
- static GLfloat ccwc[2] = { -1, 1 };
-
- static GLfloat ca[3] = { 1, 0, 0 };
- static GLfloat cb[3] = { 0, 1, 0 };
- static GLfloat cc[3] = { 0, 0, 1 };
-
- static GLubyte ZeroBitmap[9] = {
- 0x1c, 0x22, 0x22, 0x32, 0x2a, 0x26, 0x22, 0x22, 0x1c,
- };
-
- static GLubyte OneBitmap[9] = {
- 0x0e, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0c, 0x04,
- };
-
- static GLubyte TwoBitmap[9] = {
- 0x3e, 0x20, 0x10, 0x08, 0x04, 0x02, 0x22, 0x22, 0x1c,
- };
-
- static void Label(GLfloat first[2], GLfloat second[2], GLfloat third[2])
- {
- glColor3f(1, 1, 1);
- glRasterPos2fv(first);
- glBitmap(8, 9, 0, 0, 0, 0, ZeroBitmap);
- glRasterPos2fv(second);
- glBitmap(8, 9, 0, 0, 0, 0, OneBitmap);
- glRasterPos2fv(third);
- glBitmap(8, 9, 0, 0, 0, 0, TwoBitmap);
- }
-
- static void DrawTriangles(void)
- {
- glPushMatrix();
-
- Label(cwa, cwb, cwc);
- glBegin(GL_TRIANGLES);
- glColor3fv(ca); glVertex2fv(cwa);
- glColor3fv(cb); glVertex2fv(cwb);
- glColor3fv(cc); glVertex2fv(cwc);
- glEnd();
- glTranslatef(3.5, 0, 0);
- Label(ccwa, ccwb, ccwc);
- glBegin(GL_TRIANGLES);
- glColor3fv(ca); glVertex2fv(ccwa);
- glColor3fv(cb); glVertex2fv(ccwb);
- glColor3fv(cc); glVertex2fv(ccwc);
- glEnd();
-
- glTranslatef(-3.5, 3.5, 0);
- Label(cwa, cwc, cwb);
- glBegin(GL_TRIANGLES);
- glColor3fv(ca); glVertex2fv(cwa);
- glColor3fv(cc); glVertex2fv(cwc);
- glColor3fv(cb); glVertex2fv(cwb);
- glEnd();
- glTranslatef(3.5, 0, 0);
- Label(ccwa, ccwc, ccwb);
- glBegin(GL_TRIANGLES);
- glColor3fv(ca); glVertex2fv(ccwa);
- glColor3fv(cc); glVertex2fv(ccwc);
- glColor3fv(cb); glVertex2fv(ccwb);
- glEnd();
-
- glTranslatef(-3.5, 3.5, 0);
- Label(cwb, cwa, cwc);
- glBegin(GL_TRIANGLES);
- glColor3fv(cb); glVertex2fv(cwb);
- glColor3fv(ca); glVertex2fv(cwa);
- glColor3fv(cc); glVertex2fv(cwc);
- glEnd();
- glTranslatef(3.5, 0, 0);
- Label(ccwb, ccwa, ccwc);
- glBegin(GL_TRIANGLES);
- glColor3fv(cb); glVertex2fv(ccwb);
- glColor3fv(ca); glVertex2fv(ccwa);
- glColor3fv(cc); glVertex2fv(ccwc);
- glEnd();
-
- glTranslatef(-3.5, 3.5, 0);
- Label(cwb, cwc, cwa);
- glBegin(GL_TRIANGLES);
- glColor3fv(cb); glVertex2fv(cwb);
- glColor3fv(cc); glVertex2fv(cwc);
- glColor3fv(ca); glVertex2fv(cwa);
- glEnd();
- glTranslatef(3.5, 0, 0);
- Label(ccwb, ccwc, ccwa);
- glBegin(GL_TRIANGLES);
- glColor3fv(cb); glVertex2fv(ccwb);
- glColor3fv(cc); glVertex2fv(ccwc);
- glColor3fv(ca); glVertex2fv(ccwa);
- glEnd();
-
- glTranslatef(-3.5, 3.5, 0);
- Label(cwc, cwa, cwb);
- glBegin(GL_TRIANGLES);
- glColor3fv(cc); glVertex2fv(cwc);
- glColor3fv(ca); glVertex2fv(cwa);
- glColor3fv(cb); glVertex2fv(cwb);
- glEnd();
- glTranslatef(3.5, 0, 0);
- Label(ccwc, ccwa, ccwb);
- glBegin(GL_TRIANGLES);
- glColor3fv(cc); glVertex2fv(ccwc);
- glColor3fv(ca); glVertex2fv(ccwa);
- glColor3fv(cb); glVertex2fv(ccwb);
- glEnd();
-
- glTranslatef(-3.5, 3.5, 0);
- Label(cwc, cwb, cwa);
- glBegin(GL_TRIANGLES);
- glColor3fv(cc); glVertex2fv(cwc);
- glColor3fv(cb); glVertex2fv(cwb);
- glColor3fv(ca); glVertex2fv(cwa);
- glEnd();
- glTranslatef(3.5, 0, 0);
- Label(ccwc, ccwb, ccwa);
- glBegin(GL_TRIANGLES);
- glColor3fv(cc); glVertex2fv(ccwc);
- glColor3fv(cb); glVertex2fv(ccwb);
- glColor3fv(ca); glVertex2fv(ccwa);
- glEnd();
-
- glPopMatrix();
- }
-
- static void DoDisplay(void)
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-0.5, width - 0.5, -0.5, height - 0.5, 0.01, 1000.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- glViewport(0, 0, width, height);
- glClearColor(0.25, 0.25, 0.25, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
-
- /* Draw flat triangles */
- glShadeModel(GL_FLAT);
- glTranslatef(50, 50, -10);
- glScalef(20, 20, 1);
- DrawTriangles();
-
- /* Draw smooth triangles */
- glShadeModel(GL_SMOOTH);
- glTranslatef(8, 0, 0);
- DrawTriangles();
- }
-
- static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
- {
- if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
- return GL_TRUE;
- }
- return GL_FALSE;
- }
-
- static const char* FaceName(GLenum face)
- {
- switch (face) {
- case GL_FRONT: return "front";
- case GL_BACK: return "back";
- case GL_FRONT_AND_BACK: return "front and back";
- default: return "???";
- }
- }
-
- static const char* PolygonMode(GLenum mode)
- {
- switch (mode) {
- case GL_FILL: return "fill";
- case GL_POINT: return "point";
- case GL_LINE: return "line";
- default: return "???";
- }
- }
-
- static const char* FrontFaceName(GLenum mode)
- {
- switch (mode) {
- case GL_CW: return "CW";
- case GL_CCW: return "CCW";
- default: return "???";
- }
- }
-
- static void DisplayState(void)
- {
- GLint frontFace, cullFace, polygonMode[2];
- GLboolean cullEnabled;
-
- glGetIntegerv(GL_FRONT_FACE, &frontFace);
- glGetIntegerv(GL_CULL_FACE_MODE, &cullFace);
- glGetIntegerv(GL_POLYGON_MODE, &polygonMode[0]);
- cullEnabled = glIsEnabled(GL_CULL_FACE);
-
- printf("PolygonMode: front=%s back=%s\n",
- PolygonMode(polygonMode[0]),
- PolygonMode(polygonMode[1]));
- printf("CullFace: %s (%s)\n", FaceName(cullFace),
- cullEnabled ? "enabled" : "disabled");
- printf("FrontFace: %s\n", FrontFaceName(frontFace));
- printf("\n");
- }
-
- int main()
- {
- XVisualInfo *vi;
- Display *dpy;
- Colormap cmap;
- Window window;
- XSetWindowAttributes swa;
- GLXContext cx;
- XEvent event;
- GLboolean needDisplay;
- GLint ccw, cull;
- GLenum face;
-
- dpy = XOpenDisplay(0);
- if (!dpy) {
- fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
- return -1;
- }
-
- vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
- if (!vi) {
- fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
- getenv("DISPLAY"));
- return -1;
- }
-
- cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
- AllocNone);
- swa.border_pixel = 0;
- swa.colormap = cmap;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
- | KeyReleaseMask;
- window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
- width, height,
- 0, vi->depth, InputOutput, vi->visual,
- CWBorderPixel|CWColormap|CWEventMask, &swa);
- XSetWMColormapWindows(dpy, window, &window, 1);
- XStoreName(dpy, window, "Culling test");
- XMapWindow(dpy, window);
- XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
-
- cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
- if (!glXMakeCurrent(dpy, window, cx)) {
- fprintf(stderr, "Can't make window current to context\n");
- return -1;
- }
-
- face = GL_FRONT_AND_BACK;
- ccw = 0;
- cull = 0;
- glPolygonMode(face, GL_FILL);
- glFrontFace(GL_CW);
- glCullFace(GL_FRONT);
- glDisable(GL_CULL_FACE);
- DisplayState();
-
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glPointSize(4.0);
- glEnable(GL_POINT_SMOOTH);
-
- needDisplay = GL_TRUE;
- for (;;) {
- do {
- XNextEvent(dpy, &event);
- switch (event.type) {
- case Expose:
- needDisplay = GL_TRUE;
- break;
- case ConfigureNotify:
- width = event.xconfigure.width;
- height = event.xconfigure.height;
- needDisplay = GL_TRUE;
- break;
- case KeyPress:
- {
- char buf[100];
- int rv;
- KeySym ks;
-
- rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
- switch (ks) {
- case XK_0: face = GL_FRONT; break;
- case XK_1: face = GL_BACK; break;
- case XK_2: face = GL_FRONT_AND_BACK; break;
- case XK_p:
- case XK_P:
- glPolygonMode(face, GL_POINT);
- needDisplay = GL_TRUE;
- break;
- case XK_l:
- case XK_L:
- glPolygonMode(face, GL_LINE);
- needDisplay = GL_TRUE;
- break;
- case XK_f:
- case XK_F:
- glPolygonMode(face, GL_FILL);
- needDisplay = GL_TRUE;
- break;
- case XK_d:
- case XK_D:
- ccw = !ccw;
- glFrontFace(ccw ? GL_CCW : GL_CW);
- needDisplay = GL_TRUE;
- break;
- case XK_c:
- case XK_C:
- if (face != GL_FRONT_AND_BACK) {
- glCullFace(face);
- needDisplay = GL_TRUE;
- }
- break;
- case XK_e:
- case XK_E:
- cull = !cull;
- if (cull) {
- glEnable(GL_CULL_FACE);
- } else {
- glDisable(GL_CULL_FACE);
- }
- needDisplay = GL_TRUE;
- break;
- case XK_Escape:
- return 0;
- }
- }
- break;
- }
- } while (XPending(dpy) != 0);
-
- if (needDisplay) {
- needDisplay = GL_FALSE;
- DisplayState();
- DoDisplay();
- }
- }
- }
-